I read in file with four columns skipping line one. File is tab/space delimited, my goal is to get the last column.
I use function where parameter is char array. The function is OK as long it is working, however, it doesn't move the pointer of Char array to new location.
I cannot understand why. Or, how to move char pointer to location. It prints complete line as 1 startcoord, end coord and gene.
In the function, the gene name is printed fine, that is pointer is moved correctly.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include "funcs_combinations.h"
#define MAX_LEN 200
void get_gene_name(char temp_line[])
{
int space_count = 0; //get location of last space
size_t itr = 0; //iterate
size_t len_line = strlen(temp_line);
while (itr < len_line)
{
if ((temp_line)[itr] == ' ' || (temp_line)[itr] == '\t')
{
space_count = itr; //store space location
}
itr++;
}
printf("space location was %d\n", space_count);
temp_line = space_count + temp_line; //move pointer
printf("moved location is %d\n", temp_line);
printf("location is %s\n", temp_line); //printing works fine here but goes bad in returning
// return (temp_line);
}
//////////////////////////////
void remove_trailingspaces(char *newlines)
{
/*
This function is called while reading the first line
*/
size_t length = strlen(newlines);
size_t i = 0;
for (i = 0; i < length; i++)
{
if (newlines[i] == '\n')
{
newlines[i - 1] = '\0';
break;
}
}
}
//////////////////////////////
int main(int argc, char *argv[])
{
FILE *fptr;
char c[MAX_LEN];
int line_number = 0;
fptr = fopen(argv[1], "r");
if (fptr == NULL)
{
printf("null pointer for file opening\n");
}
while (fgets(c, MAX_LEN, fptr))
{
if (line_number > 1) //make sure header is read
{
if (strcmp(c, "\n") != 0)
{
remove_trailingspaces(c);
get_gene_name(c);
printf("new gene is %s\n", c); // it doesn't move to new location
add_node(&start, c); //only if not new line
}
}
line_number++;
}
fclose(fptr);
return 0;
}
I have input file with lines as:
CHR START END GENE
1 1234 546 GENE1
1 2234 5346 GENE12
1 4234 5246 GENE14
1 6234 5546 GENE16
I want to get last column, that is gene name. GENE1, GENE12 and such from function get_gene_name.
Edit: itr for get_gene_name (while loop )